<template>
  <div>
    <el-card>
      <div class="list-h">
        <el-button
          v-auth="`/project/meet-add`"
          type="primary"
          @click="handleAdd"
          >新增</el-button
        >
        <el-form
          :inline="true"
          :model="formInline"
          ref="formRef"
          @submit.prevent
          class="demo-form-inline"
        >
          <el-form-item label="标题" prop="titleLike">
            <el-input class="width-custom120" v-model="formInline.titleLike" />
          </el-form-item>
          <el-form-item label="所属项目" prop="projectId">
            <el-select
              class="width-custom120"
              v-model="formInline.projectId"
              filterable
              clearable
              remote
              placeholder="请输入名称搜索"
              :remote-method="getTableData"
            >
              <el-option
                v-for="item in projectList"
                :key="item.id"
                :label="item.projectName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="项目阶段" prop="projectStage">
            <el-select
              clearable
              class="width-custom120"
              v-model="formInline.projectStage"
            >
              <el-option
                v-for="item in projectStep"
                :key="item.dicCode"
                :value="item.dicCode"
                :label="item.dicValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="创建人" prop="createName">
            <el-input class="width-custom120" v-model="formInline.createName" />
          </el-form-item>
          <el-form-item label="创建时间" prop="">
            <el-date-picker
              class="width-custom220"
              v-model="timeValue"
              type="daterange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              value-format="YYYY-MM-DD hh:mm:ss"
              @change="handleChangeDate"
            />
          </el-form-item>

          <el-form-item>
            <el-button @click="handleReset">重置</el-button>
            <el-button type="primary" @click="handleSearch">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-skeleton style="height: 50vh" :rows="8" :loading="loading" animated>
        <el-table
          :data="tableData"
          border
          style="width: 100%"
          v-loading="loading"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        >
          <template #empty>
            <el-empty :image-size="150" description="暂无数据"></el-empty>
          </template>
          <el-table-column type="index" label="序号">
            <template v-slot="scope">
              <span>{{
                (pageState.pageNum - 1) * pageState.pageSize + scope.$index + 1
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="docNumber" label="编号" width="180">
          </el-table-column>
          <el-table-column
            prop="title"
            label="标题"
            show-overflow-tooltip
            min-width="140"
          >
          </el-table-column>
          <el-table-column prop="projectName" label="所属项目">
          </el-table-column>
          <el-table-column prop="projectStage" label="所属项目阶段">
            <template #default="{ row }">
              {{ formatterStatusType(row) }}
            </template>
          </el-table-column>
          <el-table-column prop="createName" label="创建人" min-width="100">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" min-width="120">
          </el-table-column>
          <el-table-column prop="summary" label="评审摘要">
            <template #default="{ row }">
              {{
                row.summary.length > 30
                  ? row.summary.substr(0, 30) + "..."
                  : row.summary
              }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="140">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="handleLook(row)"
                >查看</el-button
              >
              <el-button
                v-auth="`/project/meet-edit`"
                type="text"
                size="small"
                @click="handleEdit(row)"
                >编辑</el-button
              >
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="确定删除该条记录吗？"
                @confirm="handleDelete(row)"
              >
                <template #reference>
                  <el-button v-auth="`/project/meet-del`" type="text"
                    >删除</el-button
                  >
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <re-pagination
          v-if="total > 0"
          :total="total"
          v-model:page="pageState.pageNum"
          v-model:limit="pageState.pageSize"
          @pagination="handleChangePage"
        />
      </el-skeleton>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from "vue";
import { useRouter } from "vue-router";
import NotifMessage from "/@/utils/message/notification";
import * as api from "/@/api/meet";
import { postDictList } from "/@/api/common";
import { ProjectStageMap } from "../types";
import { postProjectList } from "/@/api/project";

let projectStep = ref([]);
let projectList = ref([]);

const initData = {
  // TODO 接口未出，后续根据字段直接替换
  titleLike: "", // 单据编号
  projectId: "", // 变更名称
  projectStage: "", // 项目名称
  createName: "", // 申请人
  createTimeStrStart: "",
  createTimeStrEnd: ""
};

let timeValue = ref(null);
const router = useRouter();

const formInline = reactive(initData);
const pageState = reactive({
  pageNum: 1,
  pageSize: 10
});

const formRef = ref<HTMLFormElement | null>(null) as any;
const tableData = ref([]);
let loading = ref<boolean>(false);
let total = ref<number>(0);
const formParams = ref<typeof initData>({ ...initData });

const getTableList = async (obj: object = {}) => {
  try {
    loading.value = true;
    const { code, data } = await api.postMeetList({
      ...obj,
      ...pageState
    });
    if (code === 200) {
      tableData.value = data.list;
      total.value = data.rows;
    }
  } catch (err) {
    console.log(err);
  } finally {
    loading.value = false;
  }
};

const handleReset = () => {
  formRef.value.resetFields();
  timeValue.value = null;
  handleChangeDate(null);
  pageState.pageNum = 1;
  formParams.value = initData;
  getTableList(formInline);
};

const handleSearch = () => {
  pageState.pageNum = 1;
  formParams.value = formInline;
  getTableList(formInline);
};

const handleChangePage = () => {
  getTableList(formParams.value);
};

const handleAdd = () => {
  router.replace({
    path: "/project/meet/detail",
    query: { mode: "add" }
  });
};

const handleEdit = row => {
  const { id } = row;
  router.replace({
    path: "/project/meet/detail",
    query: {
      id,
      mode: "edit"
    }
  });
};

const handleLook = row => {
  const { id } = row;
  router.replace({
    path: "/project/meet/detail",
    query: {
      id,
      mode: "look"
    }
  });
};

const handleDelete = async row => {
  try {
    const { code, message } = await api.postMeetDelete({
      id: row.id
    });
    if (code === 200) {
      NotifMessage.success("删除成功");
      getTableList(formParams.value);
    } else {
      NotifMessage.error(message);
    }
  } catch (err) {
    console.log(err);
  }
};

const handleChangeDate = item => {
  console.log(item, "date");
  if (item) {
    formInline.createTimeStrStart = item[0];
    formInline.createTimeStrEnd = item[1];
  } else {
    formInline.createTimeStrStart = "";
    formInline.createTimeStrEnd = "";
  }
};

const formatterStatusType = row => {
  if (!row.projectStage) return "";
  return ProjectStageMap.get(row.projectStage);
};

// 变更类型
const initProjectStep = async () => {
  try {
    const params = {
      dicType: "rd_project_stage"
    };
    const { code, data } = await postDictList(params);
    if (code === 200) {
      projectStep.value = data;
    }
  } catch (err) {
    console.log(err);
  }
};

watch(
  () => formInline.projectId,
  newVal => {
    if (!newVal) {
      projectList.value = [];
    }
  }
);

async function getTableData(query = "") {
  if (!query) {
    projectList.value = [];
    return;
  }
  try {
    const params = {
      pageNum: 1,
      pageSize: 100,
      projectNameLike: query
    };
    const { code, data } = await postProjectList(params);
    if (code === 200) {
      projectList.value = data.list.map(item => {
        return { id: item.id, projectName: item.projectName };
      });
    }
  } catch (err) {
    console.log(err);
  }
}

initProjectStep();

getTableList(formInline);
</script>
<style scoped lang="scss">
.redColor {
  width: 10px;
  height: 10px;
  background: #f5a623;
  border-radius: 50%;
  display: inline-block;
}

.greenColor {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #21d371;
  display: inline-block;
}

.grayColor {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #979797;
  display: inline-block;
}
</style>
